package com.bjesc_b2c.app.appraiset.widget.dialog;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.DialogPureTextAdapter;
import com.bjesc_b2c.app.appraiset.widget.BaseDialog;

import java.util.ArrayList;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import master_bean.DialogPureTextBean;

/**
 * Created by Jason Chen on 2017/2/23.
 */

public class PureTextDialog extends BaseDialog implements MultiItemTypeAdapter.OnItemClickListener {
    private TextView titleTv, cancelTv, sureTv;
    private RecyclerView recyclerView;
    private DialogPureTextAdapter adapter;
    private List<DialogPureTextBean> pureList;
    private String type;
    private OnSelectListener onSelectListener;
    private String index;
    private String title;
    private boolean isMultiple = false;    //是否多选
    private List<Integer> indexList = new ArrayList<>();


    public PureTextDialog(Context context, OnSelectListener onSelectListener, List<DialogPureTextBean> pureList, String title) {
        super(context);
        this.onSelectListener = onSelectListener;
        this.pureList = pureList;
        this.title = title;
        initViews();
    }

    private void initViews() {
        view = inflater.inflate(R.layout.dialog_fuel_type, null);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        cancelTv = (TextView) view.findViewById(R.id.tv_cancel);
        sureTv = (TextView) view.findViewById(R.id.tv_sure);
        titleTv = (TextView) view.findViewById(R.id.tv_title);
        adapter = new DialogPureTextAdapter(getContext(), R.layout.layout_item_dialog_puretext, pureList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        titleTv.setText(title);
        cancelTv.setOnClickListener(clickListener);
        sureTv.setOnClickListener(clickListener);
        setContentView(view);
    }

    public boolean isMultiple() {
        return isMultiple;
    }

    public void setMultiple(boolean multiple) {
        isMultiple = multiple;
    }

    public void setTitle(String title) {
        this.titleTv.setText(title);
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_cancel:            //取消
                    dismiss();
                    break;
                case R.id.tv_sure:              //确定
                    if (onSelectListener != null) {
                        if (isMultiple) { //多选
                            type = "";
                            index = "";
                            for (int i = 0; i < indexList.size(); i++) {
                                if(i != indexList.size()- 1){
                                    type += pureList.get(indexList.get(i)-1).getPuleType()+",";
                                    index += indexList.get(i)+"|";
                                    if(indexList.size()==1){
                                        index += indexList.get(i);
                                    }
                                }else{
                                    type += pureList.get(indexList.get(i)-1).getPuleType();
                                    index += indexList.get(i)+"";
                                }
                            }
                            onSelectListener.onSelect(type, index);
                        } else {//单选
                            onSelectListener.onSelect(type, index);
                        }
                    }
                    dismiss();
                    break;
            }
        }
    };

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        if (isMultiple) {     //多选
            if (indexList.contains(position + 1)) {
                pureList.get(position).setCheck(false);
                indexList.remove(position + 1);
            } else {
                pureList.get(position).setCheck(true);
                indexList.add(position + 1);
            }
        } else {  //单选

            type = pureList.get(position).getPuleType();
            for (int i = 0; i < pureList.size(); i++) {
                if (i == position) {
                    pureList.get(i).setCheck(true);
                } else {
                    pureList.get(i).setCheck(false);
                }
            }
            index = position + 1 + "";
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    public interface OnSelectListener {
        void onSelect(String fuelType, String index);
    }
}
